package com.stox.ui.javafx.charting.widget;

import javafx.beans.value.ChangeListener;
import javafx.beans.value.ObservableValue;
import javafx.scene.Cursor;
import javafx.scene.canvas.Canvas;

import org.springframework.beans.factory.config.ConfigurableBeanFactory;
import org.springframework.context.annotation.Scope;
import org.springframework.stereotype.Component;

@Component("resizableCanvas")
@Scope(ConfigurableBeanFactory.SCOPE_PROTOTYPE)
public class ResizableCanvas extends Canvas{

	private final Chart<?> chart;
	
	
	private ChangeListener<Number> changeListener = new ChangeListener<Number>(){
		@Override
		public void changed(ObservableValue<? extends Number> arg0,
				Number arg1, Number arg2) {
			chart.getChartingView().update();
		}
	};
	
	public ResizableCanvas(Chart<?> chart) {
		this.chart = chart;
		getStyleClass().add("resizable-canvas");
		widthProperty().addListener(changeListener);
		heightProperty().addListener(changeListener);
		setCursor(Cursor.NONE);
	}
	
	public Chart<?> getChart() {
		return chart;
	}
	
	 @Override
     public boolean isResizable() {
         return true;
     }

     @Override
     public double prefWidth(double height) {
         return getWidth();
     }

     @Override
     public double prefHeight(double width) {
         return getHeight();
     }
	
}
